package com.duleme.servlet;

import com.alibaba.fastjson.JSONObject;
import com.duleme.common.JsonUtils;
import com.duleme.common.EncodeUtils;
import com.duleme.entity.Admin;
import com.duleme.service.AdminService;
import com.duleme.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "AdminServlet",urlPatterns = "/admin/login")
public class AdminServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
        AdminService adminService = new AdminServiceImpl();
        int adminID=Integer.valueOf(request.getParameter("adminID"));
        String password=request.getParameter("password");
        Admin admin = adminService.login(adminID,password);
        if(admin!=null){
            request.getSession().setAttribute("admin",admin);
            response.getWriter().write("1");
        }else {
            response.getWriter().write("2");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
        AdminService adminService = new AdminServiceImpl();
        List<Admin> list = adminService.getAllAadmin();
        request.setAttribute("list",list);
        request.getRequestDispatcher("/admin/AdminList.jsp").forward(request,response);
    }
}
